package com.fsh.auth.application.intercepter;

import com.fsh.auth.application.context.AuthContextHolder;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AuthIntercepter implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // todo 判断路径，是登陆的话应该放行

        // 取出 loginId
        String loginId = request.getHeader("loginId");
        // 存入ThreadLocal 中
        AuthContextHolder.set("loginId",loginId);
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        AuthContextHolder.remove();
    }
}
